<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="园区编码" prop="parkNum">
        <el-select v-model="queryParams.parkNum" placeholder="请选择园区编码" clearable size="small">
          <el-option
            v-for="dict in parkList"
            :key="dict.id"
            :label="dict.parkName"
            :value="dict.parkNum"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="变压器" prop="transId">
        <el-select v-model="queryParams.transId" placeholder="请选择变压器名称" clearable size="small">
          <el-option
            v-for="dict in transIdList"
            :key="dict.id"
            :label="dict.num"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="监测日期" prop="checkTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.checkTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择监测日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="企业" prop="en">
        <el-input
          v-model="queryParams.en"
          placeholder="请输入企业"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="空开id" prop="swiftId">-->
        <!--<el-input-->
          <!--v-model="queryParams.swiftId"-->
          <!--placeholder="请输入空开id"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:nyTransCheck:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:nyTransCheck:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:nyTransCheck:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:nyTransCheck:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nyTransCheckList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="序号" align="center" prop="id" />-->
      <!--<el-table-column label="变压器id" align="center" prop="transId" />-->
      <el-table-column label="变压器编码" align="center" prop="transNum" />
      <el-table-column label="变压器名称" align="center" prop="transName" />
      <!--<el-table-column label="低控编号" align="center" prop="partId" />-->
      <!--<el-table-column label="低控编码" align="center" prop="partNum" />-->
      <!--<el-table-column label="低控名称" align="center" prop="partName" />-->
      <!--<el-table-column label="空开编码" align="center" prop="swiftId" />-->
      <el-table-column label="企业名称" align="center" prop="en" />
      <el-table-column label="空开名称" align="center" prop="airName" />
      <el-table-column label="申报容量" align="center" prop="enAllow" />
      <el-table-column label="线径" align="center" prop="diameter" />
      <el-table-column label="监测值(合计)" align="center" prop="value" />
      <el-table-column label="监测时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:nyTransCheck:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:nyTransCheck:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改变压器监测对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="变压器编码"  prop="transId">-->
          <!--<el-select v-model="form.transId" @change="selectTrans(form.transId)" placeholder="请选择变压器编码">-->
            <!--<el-option-->
              <!--v-for="item in transIdList"-->
              <!--:key="item.id"-->
              <!--:label="item.num"-->
              <!--:value="item.id"-->
            <!--/>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="低控编码" prop="partId">-->
          <!--<el-select v-model="form.partId" @change="selectPart(form.partId)" placeholder="请选择低压控制">-->
            <!--<el-option-->
              <!--v-for="item in partList"-->
              <!--:key="item.id"-->
              <!--:label="item.num"-->
              <!--:value="item.id"-->
            <!--/>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="企业名称" prop="enterprise">
        <el-select v-model="form.enterprise" @change="selectPart(form.enterprise)" filterable  placeholder="请选择企业">
        <el-option v-for="item in enterpriseLoads" :key="item.id" :label="item.num" :value="item.id"/>
        </el-select>
        </el-form-item>
        <el-form-item label="空开线径" prop="swiftId">
          <el-select v-model="form.swiftId" @change="selectAir(form.swiftId)" placeholder="请选择空开">
            <el-option
              v-for="item in airNumList"
              :key="item.attrId"
              :label="item.airNum"
              :value="item.attrId"
            />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="企业" prop="enterprise">-->
          <!--<el-input v-model="form.enterprise" disabled placeholder="请输入企业" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="空开id" prop="swiftId">-->
          <!--<el-input v-model="form.swiftId" placeholder="请输入空开id" />-->
        <!--</el-form-item>-->
        <el-form-item label="读数" prop="value">
          <el-input v-model="form.value" placeholder="请输入读数" />
        </el-form-item>
        <el-form-item label="抄表时间" prop="checkTime">
          <el-date-picker clearable size="small"
                          v-model="form.checkTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择抄表时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="抄表人" prop="checkBy">
          <el-input v-model="form.checkBy" placeholder="请输入抄表人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNyTransCheck, getNyTransCheck, delNyTransCheck, addNyTransCheck, updateNyTransCheck } from "@/api/smartParks/energy/nyTransCheck";
import { listNyTrans } from "@/api/smartParks/energy/nyTrans";
import { listNyTransPart } from "@/api/smartParks/energy/nyTransPart";
import { listNyTransPartArrs,listNyNyEnteriseLoad} from "@/api/smartParks/energy/nyTransPartArrs";
import {listBuiPark} from "@/api/parkAssets/enterCommunity";

export default {
  name: "NyTransCheck",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      checkTime:new Date(),
      // 总条数
      total: 0,
      // 变压器监测表格数据
      nyTransCheckList: [],
      transIdList:[],
      parkList:[],
      partList:[],
      airNumList:[],
      enterpriseLoads:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterprise: null,
        swiftNum: null,
        checkTime: null
      },
      // 表单参数
      form: {checkTime:new Date(),},
      // 表单校验
      rules: {
        enterprise:[{required: true,message: "企业不能为空", trigger: "change",}],
        value:[{required: true,message: "读数不能为空", trigger: "change",}],
        checkTime:[{required: true,message: "抄表时间不能为空", trigger: "change",}],

      }
    };
  },
  created() {
    this.getList();
    listNyTrans().then(response=>{
      response.rows.forEach((item) => {
        item.num = item.num + "==" + item.name;
      });
      this.transIdList = response.rows;
    });
    listNyNyEnteriseLoad().then(response=>{
      response.rows.forEach((item) => {
        item.num = item.entepriseName + "==" + item.approvalLoad + "==" +item.airName ;
      });
      this.enterpriseLoads = response.rows;
    });
    //查询园区列表，赋值给parkList;
    listBuiPark().then((response) =>{
      this.parkList = response.rows;
    })
  },
  methods: {
    /** 查询变压器监测列表 */
    getList() {
      this.loading = true;
      listNyTransCheck(this.queryParams).then(response => {
        this.nyTransCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    selectTrans(transId){
      const self=this;
      listNyTransPart({transId:transId,type:'0'}).then((response) =>{
        response.rows.forEach((item) => {
          item.num = item.num + "==" + item.name;
        });
        self.partList = response.rows;
      })
    },
    selectPart(partId){
      listNyTransPartArrs({partId:partId}).then((response) =>{
        response.rows.forEach((item) => {
          item.airNum = item.airNum + "==" + item.en;
        });
        this.airNumList = response.rows;
      })
    },
    selectAir(attrId){
      this.airNumList.find(item=>{
        if(item.attrId == attrId){
          console.log(item.en);
          this.form.enterprise = item.en;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterprise: null,
        swiftId: null,
        value: null,
        createBy: null,
        createTime: null,
        checkBy: null,
        checkTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加箱变监测";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNyTransCheck(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改空开监测";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNyTransCheck(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNyTransCheck(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除变压器监测编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNyTransCheck(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('energy/nyTransCheck/export', {
        ...this.queryParams
      }, `energy_nyTransCheck.xlsx`)
    }
  }
};
</script>
